package com.quark.framework.startup;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.quark.framework.base.QuarkEnvironment;
import com.quark.framework.base.QuarkGenericManager;
import com.quark.framework.configuration.QuarkConfigManager;
import com.quark.framework.database.QuarkDBExecuteManager;
import com.quark.framework.database.QuarkDBExecuterFactory;
import com.quark.framework.i18n.QuarkI18nManager;
import com.quark.framework.service.QuarkServiceManager;
import com.quark.framework.utility.QuarkLog;
import com.quark.framework.utility.QuarkUtilDateHelper;
import com.quark.framework.utility.QuarkLog.LOG_PRINT_POSITION;
import com.quark.network.service.servlet.QuarkServletPrivilegeService;
import com.quark.web.ui.logic.QuarkUiManager;

public class QuarkFrameworkLauncher {

	private List<QuarkGenericManager> managers = new ArrayList<QuarkGenericManager>();
	private static QuarkFrameworkLauncher inst = new QuarkFrameworkLauncher();
	private static long runningTime = 0;
	public static boolean registerManager(QuarkGenericManager manager)
	{
		inst.managers.add(manager);
		return true;
	}
	public static boolean go(String workPath)
	{
		QuarkLog.init(workPath);
		runningTime = System.currentTimeMillis();
		QuarkLog.splitLine1();
		QuarkLog.println("", "|", LOG_PRINT_POSITION.LOG_PRINT_CENTER);
		QuarkLog.println("Quark Framework V0.1.0", "|", LOG_PRINT_POSITION.LOG_PRINT_CENTER);
		QuarkLog.println("      by ZorroII", "|", LOG_PRINT_POSITION.LOG_PRINT_CENTER);
		QuarkLog.println("Starting up at " + QuarkUtilDateHelper.getDateTimeString(), "|", LOG_PRINT_POSITION.LOG_PRINT_CENTER);
		QuarkLog.println("", "|", LOG_PRINT_POSITION.LOG_PRINT_CENTER);
		QuarkLog.splitLine2();
		QuarkLog.println("Initializing Quark Core...");
		QuarkLog.println("Working1 at " + workPath);
		QuarkEnvironment.getInstance().setWorkingPath(workPath);				
		QuarkLog.println("Initializing Configuration...");
		QuarkConfigManager.getInstance().init();
		QuarkLog.println("Initializing DataBase...");
		QuarkDBExecuterFactory.getInstance().init();
		QuarkLog.println("Initializing Service Modules...");
		QuarkServiceManager.getInstance().init();
		QuarkLog.println("Initializing Internationalize Module...");
		QuarkI18nManager.getInstance().init();
		QuarkLog.println("Initializing Privilege Module...");
		QuarkServletPrivilegeService.refresh();
		QuarkLog.println("Initializing Maintenance System Ui Module...");
		QuarkUiManager.getInstance().init();
		QuarkLog.println("Core Initializing Done!");
		QuarkLog.splitLine2();
		QuarkLog.println("Initializing Project build on Quark Framework...");
		QuarkLog.println("Initializing Managers...");
		for(int i = 0; i < inst.managers.size(); i++)
		{
			QuarkGenericManager m = inst.managers.get(i);
			m.init();
		}
		QuarkLog.println("Project Initializing Done!");
		QuarkLog.println("Quark is Ready!");
		QuarkLog.splitLine2();
		return true;
	}
	public static boolean shutdown()
	{
		for(int i = 0; i < inst.managers.size(); i++)
		{
			QuarkGenericManager m = inst.managers.get(i);
			m.uninit();
		}
		return true;		
	}
	
	public static long getRunningDay()
	{
		return (System.currentTimeMillis() - runningTime) / (24 * 60 * 60 * 1000);
	}
}
